package cn.com.java.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.com.java.core.HttpRes;
import cn.com.java.model.Product;
import cn.com.java.model.ProductImages;
import cn.com.java.model.ProductType;
import cn.com.java.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.netty.util.internal.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("product")
@RequiredArgsConstructor
public class ProductController {


    private final ProductService productService;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public HttpRes listAll(String name) {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("status", 1);
        if(!StringUtil.isNullOrEmpty(name)){
            productQueryWrapper.like("name", name);
        }
        List<Product> list = productService.list(productQueryWrapper);
        List<Long> ids = list.stream().map(Product::getId).collect(Collectors.toList());
        if(ids.isEmpty()){
            return HttpRes.OK("查询成功", list);
        }
        Map<Long, List<ProductImages>> byimagesByIds = productService.getByimagesByIds(ids);
        list.forEach(p ->{
            if(byimagesByIds.containsKey(p.getId())){
                p.setImages(byimagesByIds.get(p.getId()));
            }
        });

        return HttpRes.OK("查询成功", list);
    }


    @PostMapping("listByPage")
    public HttpRes listByPage(@RequestBody Product product) {
        Page<Product> page = new Page<>(product.getPageIndex(), product.getPageSize());
        QueryWrapper<Product> productTypeQueryWrapper = new QueryWrapper<>();
        productTypeQueryWrapper.eq("create_by", product.getCreateBy());
        page = productService.page(page, productTypeQueryWrapper);
        List<Long> ids = page.getRecords().stream().map(Product::getId).collect(Collectors.toList());
        //获取产品 images
        if(ids.isEmpty()){
            return HttpRes.OK("查询成功", page);
        }
        Map<Long, List<ProductImages>> byimagesByIds = productService.getByimagesByIds(ids);
        page.getRecords().forEach(p ->{
            if(byimagesByIds.containsKey(p.getId())){
                p.setImages(byimagesByIds.get(p.getId()));
            }
        });
        return HttpRes.OK("查询成功", page);
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @RequestMapping("getById")
    public Product getById(Long id) {
        return productService.getById(id);
    }


    @RequestMapping("details")
    public HttpRes details(Long id) {
        return HttpRes.OK(productService.getById(id));
    }

    /**
     * 新增，忽略null字段
     *
     * @param product 新增的记录
     * @return 返回影响行数
     */
    @RequestMapping("insert")
    public HttpRes insert(@RequestBody Product product) {
        return HttpRes.OK(productService.save(product));
    }

    /**
     * 修改，忽略null字段
     *
     * @param product 修改的记录
     * @return 返回影响行数
     */
    @RequestMapping("update")
    public HttpRes update(@RequestBody Product product) {
        return HttpRes.OK(productService.updateById(product));
    }

    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    @GetMapping("delete")
    public HttpRes delete(Long id) {
        return HttpRes.OK(productService.removeById(id));
    }


    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    @GetMapping("release")
    public HttpRes release(Long id) {
        return HttpRes.OK(productService.release(id));
    }

}